package org.example.jida;

import cn.com.jit.assp.dsign.DSign;

import java.text.DateFormat;
import java.text.DateFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.Locale;


/**
 * 3.1.1Attach签名/Attach验签名(原型1) ok
 * 直接读取字符串内容，然后再将解密后的数据打印出来
 * 读取字符串，将其加密后，返回Base64类型字符串
 */
public class AttachSignTest {
    public static void main(String[] args) {
        String cssconfig = "E:/root/epolicy/epolicyModel/cssconfig/2.properties";
        DSign signDs = new DSign();
        signDs.initConfig(cssconfig); //通过配置文件读取参数
        //String strFileNameIn = "d:/todo.pdf";    // 待签名的文件路径

        byte[] srccode = "待签名的原文".getBytes();       // 需要签名的原文
        String signReturn = "";                          //返回签名结果
        /*****************attachSign签名*********************************/
        signReturn = signDs.attachSign("", srccode);
        if (signReturn!=null) {                           //签名成功
            System.out.println("****attach签名成功****");
            System.out.println("签名结果：" + signReturn);
        } else {                                        //签名失败
            System.out.println("错误号为：" + signDs.getErrorCode());
            System.out.println("错误描述为: " + signDs.getErrorMessage());
        }


        /***************** verifyAttachedSign验签名***********************/
        DSign vSignDs = new DSign();
        vSignDs.initConfig(cssconfig);
        long a = vSignDs.verifyAttachedSign(signReturn.getBytes());
        if (a == 0) {
            System.out.println("版本号:"+vSignDs.getCertInfo("VS", 3, ""));
            System.out.println("颁发者:"+vSignDs.getCertInfo("VS", 1, ""));
            System.out.println("证书主题:"+vSignDs.getCertInfo("VS", 0, ""));
            System.out.println("返回签名原文: " + vSignDs.getData());
            //签名结果带时间戳
            DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", new DateFormatSymbols(Locale.SIMPLIFIED_CHINESE));
            String signedTimeStr = "";
            if(vSignDs.getTsaTime() != null){
                signedTimeStr = formatter.format(vSignDs.getTsaTime());
            }
            System.out.println("时间: " + signedTimeStr);
            System.out.println("证书subjectdn: " + vSignDs.getTsaCertInfo("subjectdn"));
            System.out.println("证书issure： " + vSignDs.getTsaCertInfo("issuerdn"));
            System.out.println("证书SN： " + vSignDs.getTsaCertInfo("serialnumber"));
        } else {
            System.out.println("错误号为:" + vSignDs.getErrorCode());
            System.out.println("错误描述:" + vSignDs.getErrorMessage());
        }
    }
}